// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import store from '@/store' //vuex
import "babel-polyfill"
import {projectName} from './constant/constant.js';
import {getSession, setSession,removeSession} from './utils/MUtils';
import ajax from './api/ajax';
import mixinFunc from './mixinFunc';

Vue.prototype.projectName = projectName
Vue.prototype.ajax = ajax
Vue.prototype.setSession = setSession
Vue.prototype.getSession = getSession
Vue.prototype.removeSession = removeSession
Vue.config.productionTip = false
router.beforeEach((to, from, next) => {
  /**
   * 未登录则跳转到登录页
   * 未登录跳转到登录页,也可以通过axios的响应拦截器去实现,但是会先在当前页面渲染一下,再跳转到登录页,会有个闪动的现象
   * 这里通过路由守卫的方式,不会在当前页闪现一下,但是需要在每个路由组件添加一个是否需要登录的标识位,如本项目中的requireAuth字段
   */
  if (to.meta.requireAuth) {
    //判断是否要登录才能打开页面
    // let token = window.sessionStorage.getItem(projectName+"Token");
    let token = getSession(projectName + "Token");
    if (token) {    //有登录状态,直接跳转
      // debugger
      next()
    } else {
      next({
        path: '/Login'               //无登录状态,先跳转到登录页面
      })
    }
  } else {
    next()
  }
})
Vue.use(ElementUI);
Vue.mixin(mixinFunc);
new Vue({
  el: '#app',
  router,
  store,
  components: {App},
  template: '<App/>'
});
